<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ref的用法2-组件上加ref</title>
</head>

<body>
    <div id="root">
        <counter 
            @change="handleChange"
            ref="one">//父组件监听
        </counter>
        <counter
            @change="handleChange"
            ref="two">
        </counter>
        <div>{{total}}</div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        Vue.component('counter',{
            data:function(){
                return {
                    number:0
                }
            },
            template:'<div @click="handleClick">{{number}}</div>',
            methods:{
                handleClick:function(){
                    this.number++;
                    this.$emit("change");//子组件向父组件传值
                }
            }
        })
        var vm = new Vue({
            el: "#root",
            data:{
                total:0
            },
            methods:{
                handleChange:function(){
                    console.log(this.$refs.one.number);
                    console.log(this.$refs.two.number);
                    this.total=this.$refs.one.number+this.$refs.two.number;
                }
            }
        })
    </script>
</body>

</html>